package com.offcn.webui.controller;

import com.offcn.common.response.AppResponse;
import com.offcn.webui.service.UserFeignService;
import com.offcn.webui.vo.resp.UserRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserFeignService userService;

    @RequestMapping("/showLogin")
    public String showLogin(){
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(String loginacct, String password, HttpSession session){
        AppResponse<UserRespVo> response = userService.login(loginacct, password);
        UserRespVo user = response.getData();
        if(user == null){
            // 用户名或密码错误
            return "redirect:/login.html";
        }else{
            // 登录成功
            session.setAttribute("user",user);
            String url = (String)session.getAttribute("url");
            if (url != null) {
                return "redirect:/" + url;
            }
            return "redirect:/";
        }

    }


}
